package cn.itsource.org.controller;

import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Dictionary;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.DictionaryQuery;
import cn.itsource.org.service.IDictionaryService;
import cn.itsource.utli.AjaxResult;
import cn.itsource.utli.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    /**
     * 查找所有
     * @return
     */
    @GetMapping
    public List<Dictionary> getAll(){
        return  dictionaryService.queryAll();
    }

    /**
     * 新增修改
     * @param dictionary
     * @return
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Dictionary dictionary) {
        try {
            if (dictionary.getId() == null){
                dictionaryService.save(dictionary);
            }else {
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Dictionary queryById(@PathVariable("id") Long id){
        return dictionaryService.queryById(id);
    }

    /**
     * 分页
     * @param query
     * @return
     */
    @PostMapping
    public PageInfo<Dictionary> page(@RequestBody DictionaryQuery query){
        return  dictionaryService.queryPage(query);
    }

    @GetMapping("/deptTree")
    public List<Dictionary> getDeptTree(){
        return dictionaryService.getDeptTree();
    }

}
